package com.ruoyi.mall.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.service.ChatGPTService;
import com.ruoyi.wechat.domain.WxUser;
import com.ruoyi.wechat.mapper.WeChatLoginUser;
import com.swordintent.chatgpt.ChatgptClient;
import com.swordintent.chatgpt.ChatgptClientImpl;
import com.swordintent.chatgpt.protocol.ChatGptConfig;
import com.swordintent.chatgpt.protocol.ChatRequest;
import com.swordintent.chatgpt.protocol.ChatResponse;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import com.theokanning.openai.service.OpenAiService;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import   com.theokanning.openai.service.OpenAiService.*;

import static com.theokanning.openai.service.OpenAiService.*;

import  retrofit2.Retrofit;
import   com.theokanning.openai.OpenAiApi;

/**
 * @创建人：xiaobao
 * @创建时间： 2023/2/17.
 * @描述：
 */
@Service
public class ChatGPTServiceImpl implements ChatGPTService {

    @Autowired
    private   RedisCache redisCache;


    @Override
    public List<String> chat(String prompt, WxUser user) {
//        ChatgptClient chatgptClient = ChatgptClientImpl.getInstance();
//        ChatGptConfig chatGptConfig = ChatGptConfig.builder()
//                .email("3266802642@qq.com")
//                .password("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")
//                .build();
//        String address = "http://124.223.87.47:5000";
//        ChatResponse response = null;
//
//        String conversationId = redisCache.getCacheObject("ConversationId:"+user.getUser().getOpenid());
//        try {
//        if (StringUtils.isEmpty(conversationId)){
//            chatgptClient.init(address, chatGptConfig);
//
//            ChatRequest gptRequest = ChatRequest.builder()
//                    .prompt(prompt)
//                    .conversationId(null)
//                    .build();
//            response = chatgptClient.chat(gptRequest);
//            redisCache.setCacheObject("ConversationId:"+user.getUser().getOpenid(),response.getConversationId(), 30,TimeUnit.MINUTES);
//        }
//        else {
//            ChatRequest gptRequest = ChatRequest.builder()
//                    .prompt(prompt)
//                    .conversationId(conversationId)
//                    .build();
//            response = chatgptClient.chat(gptRequest);
//        }
//
//
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//
//        return response.getMessage();
        //设置环境变量
//        OpenAiService service = new OpenAiService("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");
//        CompletionRequest completionRequest = CompletionRequest.builder()
//                .prompt(prompt)
//                .model("text-davinci-003")
//                .echo(true).maxTokens(1000)
//                .build();
//        List<CompletionChoice> resp = service.createCompletion(completionRequest).getChoices();

        ObjectMapper mapper = defaultObjectMapper();
        OkHttpClient client = defaultClient("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", Duration.ofMillis(60000L))
                .newBuilder()
                .build();

        Retrofit retrofit = defaultRetrofit(client, mapper);
        OpenAiApi api = retrofit.create(OpenAiApi.class);
        OpenAiService service = new OpenAiService(api);
                CompletionRequest completionRequest = CompletionRequest.builder()
                .prompt(prompt)
                .model("text-davinci-003")
                .echo(false).maxTokens(1000)
                .build();
        CompletionResult resp = service.createCompletion(completionRequest);
        return resp.getChoices().stream().map(completionChoice -> completionChoice.getText()).collect(Collectors.toList());
    }
}
